<?php
namespace plugins\sms_account\controller;
/**
 * @Author: user
 * @Date:   2019-03-07 16:21:19
 * @Last Modified by:   user
 * @Last Modified time: 2019-03-20 09:56:01
 */
use cmf\controller\PluginRestBaseController;//引用插件基类

use plugins\sms_account\model\SmsAccountModel;
use plugins\sms_account\model\SmsAccountBuyModel;
use plugins\sms_account\model\SmsAccountConsumeModel;

/**
 * api控制器
 */
class ApiIndexController extends PluginRestBaseController
{
    public function index($isModule=false)//index(命名规范)
    {
        $param=$this->request->post();
        $param = zy_decodeData($param,$isModule);

        return zy_array(true,'连入成功',$param,200,$isModule);
    }


    /**
     * [获取密钥]
     * @param  string $sms_uid      [短信用户id]
     * @param  string $sms_account  [短信用户帐号]
     * @return array|json            [description]
     */
    public function getSmsKey($sms_uid="", $sms_account="", $isModule=false)
    {
        $accountInfo = SmsAccountModel::verifyAccount($sms_uid,$sms_account);
        if($accountInfo['status']=='error'){
            return zy_array(false,$accountInfo['message'],'',0,$isModule);
        }

        $data['password'] = $accountInfo['data']['password'];
        $data['signature'] = $accountInfo['data']['signature'];
        return zy_array(true,'查询成功:',$data,200,$isModule);
    }

    /**
     * [添加消费记录]
     * @param  string $sms_uid      [短信用户id]
     * @param  string $sms_account  [短信用户帐号]
     * @param  int $mobile          [接收者手机号]
     * @param  string $msg          [短信内容]
     * @param  string $ip           [发送者ip]
     * @return array|json            [description]
     */
    public function addSmsConsume($sms_uid="", $sms_account="", $mobile=0, $msg='', $ip='', $isModule=false)
    {
        $accountInfo = SmsAccountModel::verifyAccount($sms_uid,$sms_account);
        if($accountInfo['status']=='error'){
            return zy_array(false,$accountInfo['message'],'',0,$isModule);
        }
        $accountInfo = $accountInfo['data'];

        SmsAccountModel::where('id',$accountInfo['id'])->setInc('consume_num');

        $consumeData = [
            'uid'       =>$accountInfo['uid'],
            'accountid' =>$accountInfo['id'],
            'mobile'    =>$mobile,
            'msg'       => is_array( $msg ) ? json_encode( $msg ) : '',
            'ip'        =>$ip
        ];
        if( SmsAccountConsumeModel::create($consumeData) ){
            return zy_array(true,'操作成功','',200,$isModule);
        }else{
            return zy_array(false,'操作失败','',0,$isModule);
        }


    }


    /**
     * 卓远短信发送
     */
    public function sendSmsByZyAccount()
    {
        $param = $this->request->param();
		/*if( isset( $param [ 'account' ] ) && !empty( $param [ 'account' ] ) ){
			
		}*/
        $rule = [
            'account'       => 'require',
            'account_pass'  => 'require',
            'mobile'        => 'require',
            'temp_id'       => 'require',
        ];
        $msg = [
            'account.require'        => '账号不能为空',
            'account_pass.require'   => '账号密码不能为空',
            'mobile.require'         => '手机号不能为空',
            'temp_id.require'        => '模板号不能为空',
        ];

        $validate = new \think\Validate( $rule, $msg );

        
        if( !$validate->check( $param ) ){
            return api_json_echo( false , $validate->getError() , null , 102 );
        }

        $account = $param [ 'account' ];
        $pwd     = $param [ 'account_pass' ];
        $find    = SmsAccountModel::where( [ 'account' => $account , 'password' => $pwd ] )->find();
        if( !$find ){
            return api_json_echo( false , '账号或密码不正确' , null , 101 );
        }

        $mobile  = $param [ 'mobile' ];
        $temp_id  = $param [ 'temp_id' ];
        $content = isset( $param [ 'content' ] ) ? $param [ 'content' ] : '' ;
        $sign    = $find [ 'signature' ] ;

        $key     = "LTAINNjf3ubqn5cw";
        $key_secret = "uLEHmWoH8RCnXKP9XLH9skE0qIfwVW";

        //发送
        // 引入短信发送类
        \think\Loader::import('aliyunsms', APP_PATH."zyapi/lib/zy_message/Aliyunsms", '.class.php');
        //实例化短信发送
        $sms = new \SmsDemo( $key , $key_secret , $sign );

        //单发
        $res=$sms->sendSms( $mobile , $temp_id , $content );

        if( $res->Code=='OK' ){
            $this->addSmsConsume( $find [ 'uid' ] , $account , $mobile , $content );
            return api_json_echo( true , '发送成功' );
        }
        return api_json_echo( false , $res->Message , null , 100 );

    }
	
	
	//第三方短信发送 接口
	
	public function otherSms()
	{
		
		$ip = "118.178.188.81";
		//die("45415");
		$post_data = array();
		$post_data['account'] = "tianyan_zhuoyuan";
		$post_data['pswd'] = "Zhuoyuan12";
		//$post_data['account'] = 'zhouy_zywl';
		//$post_data['pawd'] = 'ZhuoYuanwl666';
		$post_data['mobile'] ="15858620686";
		$post_data['msg']= "【温州市天音心理咨询中心】尊敬的用户您好，您的验证码为：606404，验证码有效期为5分钟。"; 
		$post_data['needstatus'] ="true";
		$url='http://'.$ip.'/msg/HttpBatchSendSM';
		$o="";
		foreach ($post_data as $k=>$v)
		{
		   $o.= "$k=".urlencode($v)."&";
		}
		$post_data=substr($o,0,-1);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$result = curl_exec($ch);
	}
	
	
	
	
	
	
	
	
	
    
}